<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
    //    echo putFile("utorrent.exe");

        echo getFile(3, "d:/xYYxxs");
      //  echo delFile(5);

        function delFile($code)
        {
            $csock = fsockopen("127.0.0.1", 4077) or die("cannot connect to server");
            $dsock = fsockopen("127.0.0.1", 4078) or die("cannot connect to server");

            if(!$csock || !$dsock)
                return -1;

            fwrite($csock, "del:".$code."\n");
            fflush($csock);
            return split(":", fgets($csock));
        }

        function getFile($code,$tmpFile)
        {
            $csock = fsockopen("127.0.0.1", 4077) or die("cannot connect to server");
            $dsock = fsockopen("127.0.0.1", 4078) or die("cannot connect to server");

            if(!$csock || !$dsock)
                return -1;

            fwrite($csock, "get:".$code."\n");
            fflush($csock);

            if(!is_dir($tmpFile))
                mkdir($tmpFile);

            $valueArr = split(":", fgets($csock));
            $fileSize = $valueArr[1];

            $fcount = 0;
            while(is_file($tmpFile."/".$fcount."".$valueArr[0]))
            {
                $fcount++;
            }

            $handle = fopen($tmpFile."/".$fcount."".$valueArr[0],"w") or die("cannot open datafile");
        
            $count = 0;
            $len = 1024;

            while ($count < $fileSize)
            {
               $contents = null;
               if($fileSize - $count < $len)
                   $len = $fileSize - $count;
               
               $contents = fread($dsock, $len) or die("cannot fget");
               fwrite($handle, $contents) or die("cannnot fput");
               $count += $len;
            }
            fclose($dsock);
            fclose($handle);
            fclose($csock) or die("cannot close socket");

            return $tmpFile."/".$fcount."".$valueArr[0];
        }

        function putFile($filename)
        {

            $csock = fsockopen("127.0.0.1", 4077) or die("cannot connect to server");
            $dsock = fsockopen("127.0.0.1", 4078) or die("cannot connect to server");

            $fileSize = filesize($filename);
            if(!$csock || !$dsock)
                return -1;

            fwrite($csock, "put:".$fileSize.":".$filename."\n");
            fflush($csock);

            $handle = fopen($filename, "rb") or die("cannot open datafile");
        
            $count = 0;
            $len = 1024;

            while ($count < $fileSize)
            {
               $contents = null;
               if($fileSize - $count < $len)
                   $len = $fileSize - $count;
               
               $contents = fread($handle, $len) or die("cannot fget");
               fwrite($dsock, $contents) or die("cannnot fput");
               $count += $len;
            }
            fclose($dsock);

            $fileIndex = fgets($csock) or die("cannot fread");
 
            fclose($handle) or die("cannot close datafile");
            fclose($csock) or die("cannot close socket");

            return $fileIndex;
        }

        ?>
    </body>
</html>
